package com.baizhi.reflect2;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 通过反射获取构造函数来创建对象
 */
public class ConstructorDemo2 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //1. 获取类的对象
        Class<Person> pClass = Person.class;

        //2.获取对应的构造函数
        Constructor<Person> con = pClass.getConstructor(String.class, int.class);

        //3. 创建对象
        Person p = con.newInstance("小明", 22);
        System.out.println(p);

    }
}
